<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-11-05
 * Time: 16:37
 */
namespace app\index\controller;

use app\src\alipay\action\AlipayNotifyAction;
use app\src\alipay\po\AlipayNotifyPo;
use app\src\base\utils\BoyeService;
use think\Controller;

// use app\src\alipay\po\AlipayNotfiyPo;
/**
 * 支付宝
 * Class Alipay
 * @author hebidu <email:346551990@qq.com>
 * @package app\index\controller
 */
class Alipay extends Controller{


    public function notify(){

        $arr   = $_POST;
        $debug = false;
        if($debug){
            $arr = '{"gmt_create":"2017-10-27 11:31:01","charset":"utf-8","seller_email":"343023720@qq.com","subject":"\u6709\u8f66\u6709\u4fdd\u652f\u4ed8\u5b9d\u652f\u4ed8","sign":"RhjNyiVoOAPcyeVD9mHktoAMNd\/7R4seZ6Tc5xQYaSRsUP5+jaOg\/ShV9WJyksn71lo7C+t89ndelK2MbkcIvy7MsblODK5DPg6rZTPkFC9gxab3d2Al5WIaOp4uqYU1roH3xqN9iISOgPw0z8B2wSJ48iG+LDsdoU+qSsV2JwQfJfYLcdtpv24sUVkvOH82OnMc+N9PjnoTuXaq23A\/05P8kIeTt19db\/q3FpBKWMZoofSn7qvKxt4T6rdZR8OdXqonvd8e5keoFnF7HYkZOcRh1vrP\/nres4idUhoFeJn7T7gUiRKBcy09UU2HSTvMkF3lAHXvw9+BgvC3K2eEvQ==","body":"\u6709\u8f66\u6709\u4fdd\u652f\u4ed8\u5b9d\u652f\u4ed8","buyer_id":"2088802842828925","invoice_amount":"0.01","notify_id":"97d23ef63994b664799012a220de238n3m","fund_bill_list":"[{\"amount\":\"0.01\",\"fundChannel\":\"ALIPAYACCOUNT\"}]","notify_type":"trade_status_sync","trade_status":"TRADE_SUCCESS","receipt_amount":"0.01","app_id":"2017101609337169","buyer_pay_amount":"0.01","sign_type":"RSA2","seller_id":"2088821288423691","gmt_payment":"2017-10-27 11:31:02","notify_time":"2017-10-27 11:31:02","version":"1.0","out_trade_no":"SELL2017102711P364","total_amount":"0.01","trade_no":"2017102721001004920247170450","auth_app_id":"2017101609337169","buyer_logon_id":"152****1285","point_amount":"0.00"}';
        }

        //$arr ='{"gmt_create":"2017-08-21 13:45:14","charset":"utf-8","seller_email":"1280774893@qq.com","subject":"SELL2017082113P250","sign":"VrJDZf8VJgkdZ1k828UXvCu8Mywyv8k0JkBoBlKgSKekiGZGMw6h6Z2s+TKbiCDDSZqWM5\/pqy5P+vNfAhz6iE0Pi0qZ3uBy47P1FxIkLnorHi6bT+gyZwBvvg6\/k7PESVaLyLPbsNzY\/fsUs3Du+VxFn5P7cL5F5WU\/GW0QNkHAIBvC\/TtUOduvr2Cq96qfZshdz3W0rJGTDZh654tUV5XVtqewi4k2EpLLotu6MPjcRDuTwlGReMngVFCrSymZmY\/YXKZimcxmU42furhdz6yeraye1D8r0Pcr50lYgxJYqIynJlSCswX8c4JxeS4x4ULen4vY43UPpSHZ9jxJMg==","body":"\u8f66\u5168\u4fdd","buyer_id":"2088802495263689","invoice_amount":"0.01","notify_id":"c99996d003a8f018c90cc9aa9f78c1al8y","fund_bill_list":"[{\"amount\":\"0.01\",\"fundChannel\":\"ALIPAYACCOUNT\"}]","notify_type":"trade_status_sync","trade_status":"TRADE_SUCCESS","receipt_amount":"0.01","app_id":"2017050907173527","buyer_pay_amount":"0.01","sign_type":"RSA2","seller_id":"2088621976613760","gmt_payment":"2017-08-21 13:45:14","notify_time":"2017-08-21 13:45:15","version":"1.0","out_trade_no":"SELL2017082113P250","total_amount":"0.01","trade_no":"2017082121001004680207386656","auth_app_id":"2017050907173527","buyer_logon_id":"188****6998","point_amount":"0.00"}';
        addLog("Alipay_notify",$_GET,$arr,"支付宝异步通知");
//        dump($arr);exit;
        //$arr = json_decode($arr,true);

        $alipayNotfiyPo = new AlipayNotifyPo();
        $alipayNotfiyPo->init($arr);
        $action = new AlipayNotifyAction($alipayNotfiyPo);
        addLog("Alipay_notify",'运行到此处',$arr,"支付宝异步通知");
        session('post',$arr);
        $result =  $action->notify($debug);
        //return($result);
        echo $result['info'];
    }

    public function test(){
        $arr='{"gmt_create":"2017-10-23 09:17:18","charset":"utf-8","seller_email":"343023720@qq.com","subject":"\u8f66\u5168\u5b9d\u652f\u4ed8\u5b9d\u652f\u4ed8","sign":"FnQ5ZS5KwIy3+zVfQIpfi3DfMyF6KpS9T3Qf51tO28C3VX6IXCyPp\/1LK32ukIgxM6ocCkHrO0nF\/MUz6jSlgh7Xxc6j4rgAN1SNQ6\/2jm3U1hrFe4LRao0pU79DF83DChqwkBX38LEZ+6jtGXKOu\/iq\/q8Fw3KmZX4yGFo5Bb6TZ8OvNqxx+dHlMeCWZ3yG5gqsDOrUhPbXpDC4BLcDX49JEcWFbnhc0Pg5jXqu\/X2Q2zqgD8danBK76F3Iqf5\/+JEfO+gw6GqbfyJEhyb80PY4sV2HP7bGP+u6CRPZAsXp5Na04NTOeg8v8MeC\/YtOoy5ex056yhIbjaGGpJbhFA==","body":"\u8f66\u5168\u5b9d\u652f\u4ed8\u5b9d\u652f\u4ed8","buyer_id":"2088802842828925","invoice_amount":"0.01","notify_id":"4f9c97bef720b1d07af52b6db72c5d9n3m","fund_bill_list":"[{\"amount\":\"0.01\",\"fundChannel\":\"ALIPAYACCOUNT\"}]","notify_type":"trade_status_sync","trade_status":"TRADE_SUCCESS","receipt_amount":"0.01","app_id":"2017101609337169","buyer_pay_amount":"0.01","sign_type":"RSA2","seller_id":"2088821288423691","gmt_payment":"2017-10-23 09:17:18","notify_time":"2017-10-23 09:17:19","version":"1.0","out_trade_no":"SELL2017102309P2","total_amount":"0.01","trade_no":"2017102321001004920236126880","auth_app_id":"2017101609337169","buyer_logon_id":"152****1285","point_amount":"0.00"}';
        $alipayNotfiyPo = new AlipayNotifyPo();
        $alipayNotfiyPo->init( json_decode($arr,true));
        $action = new AlipayNotifyAction($alipayNotfiyPo);
        $result = $action->test(json_decode($arr,true),json_decode($arr,true)['sign']);
        dump($result);
    }


    public function curl(){
        $data ='{"gmt_create":"2017-08-21 14:10:10","charset":"utf-8","seller_email":"1280774893@qq.com","subject":"SELL2017082114P251","sign":"XxoB2wpRIUGIQsomrQtoMQKvChh2BdR64BUrIy9vtS2jq0fA3Ly9OVI2HNVnGFykATnuR0bxmHzB7G0QnG8claMV4BUP0ZD+te5+NcREUn8nDAMES0vSDI1Hmok+AEfskeVZl4NA0igZq92jrvokVb2hoQ3nkyi0TLVVefmSVqRfEVNdi1ZkbrwgINdKcEwAxclbMsst53Wzlk+Kss2SmQxBYhKOTWmOUleOyz0mbdPpYNqkToASy\/WIdA9FAQN+CR3t4d+lG0WdaAH+\/nLYxse0x4Q\/Kx5KH\/2wkkJuql6OvRlKea4B\/OKkSEjQ0eAgEP0r1lPHG9gTbneNRDeT1Q==","body":"\u8f66\u5168\u4fdd","buyer_id":"2088802495263689","invoice_amount":"0.01","notify_id":"e217480c00739dadb3bcdd8d76a4c93l8y","fund_bill_list":"[{\"amount\":\"0.01\",\"fundChannel\":\"ALIPAYACCOUNT\"}]","notify_type":"trade_status_sync","trade_status":"TRADE_SUCCESS","receipt_amount":"0.01","app_id":"2017050907173527","buyer_pay_amount":"0.01","sign_type":"RSA2","seller_id":"2088621976613760","gmt_payment":"2017-08-21 14:10:11","notify_time":"2017-08-21 14:10:11","version":"1.0","out_trade_no":"SELL2017082114P251","total_amount":"0.01","trade_no":"2017082121001004680207474655","auth_app_id":"2017050907173527","buyer_logon_id":"188****6998","point_amount":"0.00"}';
        $data = json_decode($data,true);
        $url = 'http://api.zhangwo-ltd.com/index.php/alipay/notify';
//        $url = $this->apiUrl.$url;
//        $url = rtrim($url,"/");
        //对data进行加密
        $ch     = curl_init();
        $header = ['Accept-Charset'=>"utf-8"];
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.64 Safari/537.36'); //chrome46/mac
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, ($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        $errorno = curl_errno($ch);
        dump($tmpInfo);exit;



    }


}